﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Windows.Controls;
using CThru.Silverlight;

namespace DefaultContentProvider.Test.Unit
{
    public class DefaultContentProviderTests
    {
        private DefaultContentProvider contentProvider;

        [SetUp]
        public void Setup()
        {
            contentProvider = new DefaultContentProvider();
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void IsItemTypeSupported_NullPassed_ThrowArgumentNullException()
        {
            contentProvider.IsItemTypeSupported(null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetItemsMapping_NullPassed_ThrowArgumentNullException()
        {
            contentProvider.GetItemsMapping(null);
        }

        [Test]
        public void IsItemTypeSupported_NotKnownTypePassesd_ReturnsFalse()
        {
            var result = contentProvider.IsItemTypeSupported("agegebeb");

            Assert.False(result);
        }

        [Test]
        [ExpectedException(typeof(KeyNotFoundException))]
        public void GetItemsMapping_NotKnownTypePassesd_FunctionThrowsKeyNotFound()
        {
            var mapping = contentProvider.GetItemsMapping("agegebeb");

            var control = mapping("http://imageworld.com/img/123");
        }

        [Test, SilverlightUnitTest]
        public void IsItemTypeSupported_StandardItemsMapping_AllReturnsTrue()
        {
            var textMapping = contentProvider.IsItemTypeSupported("text");
            Assert.True(textMapping);

            var linkMapping = contentProvider.IsItemTypeSupported("link");
            Assert.True(linkMapping);

            var imageMapping = contentProvider.IsItemTypeSupported("image");
            Assert.True(imageMapping);
        }

        [Test, SilverlightUnitTest]
        public void GetItemsMapping_StandardItemsMapping_AllReturnsProperValue()
        {
            var textMapping = contentProvider.GetItemsMapping("text");
            var textControl = textMapping("This is ordinary text");
            Assert.IsInstanceOf(typeof(TextBlock), textControl);

            var linkMapping = contentProvider.GetItemsMapping("link");
            var linkControl = linkMapping("http://textworld/article/5");
            Assert.IsInstanceOf(typeof(HyperlinkButton), linkControl);

            var imageMapping = contentProvider.GetItemsMapping("image");
            var imageControl = imageMapping("http://imageworld.com/img/123.jpg");
            Assert.IsInstanceOf(typeof(Image), imageControl);
        }
    }
}
